package com.laiketui.plugin.common.mapper;

import com.laiketui.plugin.common.domain.base.IntegralGoodsModel;
import com.laiketui.plugin.common.tool.db.BaseMapper;
import com.laiketui.core.exception.LaiKeAPIException;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;


/**
 * 积分商城商品表 sql映射
 *
 * @author Trick
 * @date 2020/10/12 11:19
 */
public interface IntegralGoodsModelMapper extends BaseMapper<IntegralGoodsModel> {

    @Override
    List<Map<String, Object>> selectDynamic(Map<String, Object> map) throws LaiKeAPIException;

    @Override
    int countDynamic(Map<String, Object> map) throws LaiKeAPIException;

    /**
     * 获取积分商品信息
     *
     * @param map -
     * @return Map
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2020/10/12 11:21
     */
    List<Map<String, Object>> getGoodsInfo(Map<String, Object> map);

    int countGoodsInfo(Map<String, Object> map);

    /**
     * 获取积分商品信息
     *
     * @param map -
     * @return Map
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2020/10/12 11:21
     */
    List<Map<String, Object>> getGoodsInfo1(Map<String, Object> map) throws LaiKeAPIException;

    int countGoodsInfo1(Map<String, Object> map);

    /**
     * 获取积分商品明细
     *
     * @param id - 积分商品id
     * @return Map
     * @author Trick
     * @date 2021/4/14 10:53
     */
    @Select("select g.is_delete,g.integral,g.money,g.goods_id,g.attr_id, a.content,a.mch_id,a.product_title,a.volume,a.real_volume,a.status,a.imgurl," +
            "a.freight,a.initial from lkt_integral_goods as g " +
            "left join lkt_product_list as a on g.goods_id=a.id where g.id = #{id}")
    Map<String, Object> getGoodsDetailInfo(int id);

    /**
     * 获取商城最大列序号
     *
     * @param storeId -
     * @return int
     * @author Trick
     * @date 2021/5/12 14:17
     */
    @Select("select if(sort is null,0,max(sort)) from lkt_integral_goods where store_id=#{storeId} ")
    int getMaxSort(int storeId);


    @Select("select g.integral,g.money,c.img from lkt_integral_goods as g left join lkt_configure as c on g.attr_id = c.id where g.id=#{gid} ")
    Map<String,Object> getGoodsInfo2(String gid);

//    //获取商品与库存量
//    @Select("select if(a.num is null,0,sum(a.num)) from lkt_integral_goods a,lkt_product_list b where a.goods_id=#{goodsId} and a.is_delete=0 " +
//            " and a.goods_id = b.id and b.recycle=0 ")
//    int getGoodsStockNum(int goodsId);

    //获取商品与库存量
    @Select("select if(a.num is null,0,sum(a.num)) from lkt_integral_goods a where a.goods_id=#{goodsId} and a.is_delete=0 and a.attr_id = #{attrId}")
    int getGoodsStockNum(int goodsId, int attrId);
    //增减库存
    @Update("update lkt_integral_goods a set a.num=a.num + #{stockNum} where a.id=#{integralId} and a.num>=0")
    int addStockNum(int integralId,int stockNum);

    //增减库存
    @Update("update lkt_integral_goods a set a.num=a.num + #{stockNum},a.max_num = a.max_num + #{stockNum} where a.id=#{integralId} and a.num>=0")
    int addStockNum1(int integralId,int stockNum);


    //释放库存
    @Update("update lkt_integral_goods a set a.num=a.num+(select if(num is null,0,sum(num)) num from lkt_order_details b where b.p_id=#{id}) where a.id=#{id}")
    int releaseStockNum(int id);

    /**
     * 获取多规格的最低价格商品
     * @return
     */
    @Select("SELECT id,MIN(money)  FROM `lkt_integral_goods` WHERE is_delete = 0 and store_id = #{storeId} GROUP BY goods_id")
    List<Map<String, Object>> selectMinGoods(Integer storeId);

    /**
     * 获取多规格的所有规格商品数量和
     * @return
     */
    @Select("SELECT sum(num)  FROM `lkt_integral_goods` WHERE is_delete = 0 and store_id = #{storeId} and goods_id = #{goodsId}")
    Integer selectAllGoodsNum(Integer storeId, Integer goodsId);

    /**
     * 获取积分商品兑换记录
     */
    List<Map<String, Object>> getRecordsByGoodsId(Map<String, Object> map);

    /**
     * 获取积分商品兑换记录
     */
    Integer getRecordsByGoodsIdcount(Map<String, Object> map);

    /**
     * 根据商品id 规格id判断商品是否存在
     * @param storeId
     * @param goodsId
     * @return
     */
    @Select("SELECT count(0)  FROM `lkt_integral_goods` WHERE is_delete = 0 and store_id = #{storeId} and goods_id = #{goodsId} and attr_id = #{attrId}")
    Integer selectGoodsNumByGoodsIdAndAttrId(Integer storeId, Integer goodsId, Integer attrId);

    /**
     * 判断当前商品所有规格是否全部下架
     * @param storeId
     * @param goodsId
     * @return
     */
    @Select("SELECT count(0)  FROM `lkt_integral_goods` WHERE is_delete = 0 and store_id = #{storeId} and goods_id = #{goodsId} and status = 2")
    Integer selectGoodsNumByGoodsId(Integer storeId, Integer goodsId);

    /**
     * 根据 规格id商品获取积分商品剩余库存
     * @param storeId
     * @param attrId
     * @return
     */
    @Select("SELECT num  FROM `lkt_integral_goods` WHERE is_delete = 0 and store_id = #{storeId} and attr_id = #{attrId}")
    Integer selectAttrNumByAttrId(Integer storeId, Integer attrId);
}